package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sczmgk.hotline_pzh.MyApp;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.RefreshAndLoadMoreBaseActivity;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.entity.BaseEntity;
import com.sczmgk.hotline_pzh.entity.MyMessageBean.MessageInfo;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.view.NoDoubleItemClickListener;
import com.sczmgk.hotline_pzh.view.RefreshListView;

import java.util.HashMap;
import java.util.Map;

import static com.sczmgk.hotline_pzh.MyApp.bitmapUtils;

/**
 * Created by Administrator on 2016/12/8 0008.
 */
public class MyMessageActivity extends RefreshAndLoadMoreBaseActivity<MessageInfo> {

    /*空图片的控件*/
    private LinearLayout mLayoutEmpty;

    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);

        setHeader();
        initView();
        initData();
        setListener();

    }

    /**
     * 设置头
     */
    private void setHeader() {
        setTitle(R.string.my_message);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {

        //带有刷新功能的listview
        mListView = (RefreshListView) findViewById(R.id.lv_my_message);
        //空的图片的控件
        mLayoutEmpty = (LinearLayout) findViewById(R.id.ll_msg_empty);
        adapter = new MyAdapter();
        mListView.setAdapter(adapter);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        loadData(1, "", LOAD_NORMAL);
    }

    /**
     * 设置监听
     */
    public void setListener() {
        super.setListener();
        //ListView条目点击事件
        mListView.setOnItemClickListener(new NoDoubleItemClickListener() {

            @Override
            public void NoDoubleItemClickListener(AdapterView<?> parent, View view, int i, long id) {

                int newposition = i - 1;
                final MessageInfo info = lists.get(newposition);
                if (info.isread == 0) {
                    lists.get(newposition).isread = 1;
                    //发送我的消息的已读消息到服务器
                    postMessageData(info.msgid);
                }

                //跳转到消息界面
                Intent msgIntent = new Intent(MyMessageActivity.this, MessageActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(Constants.MESSAGE_DETAIL, lists.get(newposition));
                msgIntent.putExtras(bundle);
                startActivity(msgIntent);
            }
        });


        //设置消息条目的长点击
        mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {

                showDeleteDialog(position);

                return true;
            }
        });
    }

    @Override
    public Map<String, Object> getRequestData(int page, String keyword) {

        Map<String, Object> infos = new HashMap<>();
        infos.put("userid", CacheUtils.getIntData(MyMessageActivity.this, Constants.LOGIN_UID, 0));
        infos.put("page", page);
        infos.put("imei", MyApp.imei);
        return infos;
    }

    @Override
    public String getRquestUrl() {
        return Constants.BASE_URL + Constants.GET_MY_MESSAGE_URL;
    }

    @Override
    public int getListType() {
        return LIST_TYPE_UP_LOAD_MORE;
    }

    /**
     * 点击条目后传递已读消息id到服务器
     *
     * @param msgid
     */
    private void postMessageData(int msgid) {

        Map<String, Object> infos = new HashMap<>();

        infos.put("msgid", msgid);

        MyHttp.requestByCommon(Constants.BASE_URL + Constants.POST_MY_MESSAGE_URL, infos, MyHttp.POST, MyMessageActivity.this, false, null, new SimplePaserJsonCallback() {

            @Override
            public void onVerifyIsNull() {
                mHandler.sendEmptyMessage(REFRESH_LIST);
            }

            @Override
            public void onVerifySuccess(String dataRaw, boolean isCache) {
            }

        });
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case REFRESH_LIST:

                if (lists.size() == 0) {
                    mLayoutEmpty.setVisibility(View.VISIBLE);
                    mListView.setEnablePullDown(false);
                } else {
                    mLayoutEmpty.setVisibility(View.GONE);
                    mListView.setEnablePullDown(true);
                }
                adapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    public boolean getIsNeedLogin() {
        return true;
    }

    /**
     * ListView的Adapter
     */
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {

            ViewHolder holder = null;

            if (convertView == null) {
                convertView = View.inflate(MyMessageActivity.this, R.layout.item_my_message, null);
                holder = new ViewHolder();
                holder.icon = (ImageView) convertView.findViewById(R.id.iv_item_msg_icon);
                holder.redPoint = (ImageView) convertView.findViewById(R.id.iv_item_msg_point);
                holder.title = (TextView) convertView.findViewById(R.id.tv_item_msg_title);
                holder.content = (TextView) convertView.findViewById(R.id.tv_item_msg_content);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            MessageInfo info = lists.get(i);

            //处理红点的显示
            if (info.isread == 0) {
                holder.redPoint.setVisibility(View.VISIBLE);
            } else {
                holder.redPoint.setVisibility(View.INVISIBLE);
            }

            holder.title.setText(info.title);
            holder.content.setText(info.description);

            if (info.picurl != null && info.picurl.length() > 0) {
                bitmapUtils.display(holder.icon, info.picurl);
            }else{
                holder.icon.setImageResource(R.drawable.my_message_bg);
            }

            return convertView;
        }
    }

    private class ViewHolder {
        ImageView icon;
        ImageView redPoint;
        TextView title;
        TextView content;
    }

    @Override
    protected void onResume() {
        super.onResume();
        //初始化消息数据
//        loadData(1,"",LOAD_NORMAL);
    }


    /**
     * 展示长按删除的dialog
     */
    private void showDeleteDialog(final int position) {

        final int newPosition = position - 1;
        DialogFactory.chooseDialog(MyMessageActivity.this, R.string.delete_msg_des, R.string.sure, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO 长按删除收藏的消息,暂时直接弹出框从集合删除
                Map<String, Object> maps = new HashMap<String, Object>();
                maps.put("id", lists.get(newPosition).msgid);
                MyHttp.requestByCommon(Constants.BASE_URL + Constants.POST_DELETE_MSG_URL, maps, MyHttp.POST, MyMessageActivity.this, false, null, new SimplePaserJsonCallback() {
                    @Override
                    public void onVerifyIsNull() {
                        lists.remove(newPosition);
                        mHandler.sendEmptyMessage(REFRESH_LIST);
                    }

                    @Override
                    public void onVerifySuccess(String dataRaw, boolean isCache) {

                    }

                    @Override
                    public void onErrorCodeError(BaseEntity.MyError mError) {
                        super.onErrorCodeError(mError);
                        DialogFactory.warningDialog(MyMessageActivity.this, mError.msg);
                    }
                });

            }
        });
    }
}
